package com.sunny.controller;

import com.sunny.crawler.processor.FinanceProcessor;
import com.sunny.common.pojo.SunResult;
import com.sunny.service.FinanceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import static com.sunny.config.BeansConfig.crawlerRunningFlag;

/**
 * @Author: sublun
 * @Date: 2021/3/19 16:38
 */
@RestController
public class FinanceController {
    @Autowired
    private FinanceProcessor financeProcessor;
    @Autowired
    private FinanceService financeService;



    //@GetMapping("/doCollectMetaData")
    public SunResult doCollectMetaData() {
        if (!crawlerRunningFlag.get()) {
            //设置爬虫状态为运行中
            crawlerRunningFlag.set(true);
            new Thread(() -> {
                financeProcessor.doProcess();
            }).start();
            return SunResult.ok();
        }
        return SunResult.message(400, "爬虫正在运行中，请勿重复执行");
    }

    @GetMapping("/createTables")
    public SunResult doCreateTables() {
        return financeService.createFinanceTables();
    }
}
